<!DOCTYPE html>
<html lang="zh_cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width='device-width', initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <img id="img" width="40" src="./img_the_scream.jpg" />
    <canvas id="canvas" width="800" height="500" style="display:block;border:solid 2px red;margin:auto"></canvas>
    <script>
        var c = document.getElementById('canvas');
        var ctx = c.getContext('2d');
        /**绘制矩形**/

        ctx.fillStyle = "#FF0000";
        ctx.fillRect(0, 0, 120, 120);

        /**绘制线条**/
        ctx.moveTo(10, 70);
        ctx.lineTo(200, 30);
        ctx.stroke();

        /**绘制圆形**/
        ctx.beginPath();
        ctx.arc(95, 50, 50, 0, 2 * Math.PI);
        ctx.stroke();

        /**绘制文本**/
        ctx.font = "20px Arial";
        ctx.fillStyle = "green"
        ctx.fillText("hello canvas", 200, 200)
        ctx.fillStyle = "green"
        ctx.strokeText('hello canvas', 230, 230)

        /**绘制渐变**/
        var grd = ctx.createLinearGradient(0, 0, 200, 0);
        grd.addColorStop(0, "red");
        grd.addColorStop(0.5, "#fff");
        grd.addColorStop(1, "blue");
        ctx.fillStyle = grd;
        ctx.fillRect(10, 130, 150, 80);

        /**绘制图片**/
        // var img = document.createElement("img");
        // img.src = "./img_the_scream.jpg";
        // document.body.appendChild(img);
        document.getElementById("img").onload = function() {
            ctx.drawImage(this, 150, 100, 40, 100)
        }
    </script>
</body>

</html>